package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService{
	@Autowired
	private EmployeeMapper employeeMapper;
	@Override
	public void insert(Employee employee, Long[] roleIds) {
		String plainPassword = employee.getPassword();
		String cipherPassword = new SimpleHash("MD5", plainPassword, employee.getName()).toString();
		employee.setPassword(cipherPassword);
		employeeMapper.insert(employee);

		//往中间表插入数据
		if (roleIds != null) {
			for (Long roleId : roleIds) {
				employeeMapper.insertRelation(employee.getId(),roleId);
			}
		}
	}

	@Override
	public void delete(Long id) {
		employeeMapper.deleteRelation(id);
		employeeMapper.delete(id);
	}

	@Override
	public void update(Employee employee, Long[] roleIds) {
		employeeMapper.update(employee);
		//更新中间表需要先删除该员工在中间表的相关数据再根据roleIds插入新的数据完成更新
		employeeMapper.deleteRelation(employee.getId());
		if (roleIds != null &&roleIds.length>0) {
			for (Long roleId : roleIds) {
				employeeMapper.insertRelation(employee.getId(),roleId);
			}
		}
	}

	@Override
	public Employee select(Long id) {
		Employee employee = employeeMapper.select(id);
		return employee;
	}

	@Override
	public List<Employee> list() {
		List<Employee> list = employeeMapper.list();
		return list;
	}

	public PageInfo<Employee> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<Employee>(employees);
	}

	public Employee login(String username, String password) {
		Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);

		return employee;
	}

	public void batchDelete(Long[] ids) {
		employeeMapper.batchDelete(ids);
	}

	public Employee queryEmployeeByUsername(String username) {

		return employeeMapper.selectByUsername(username);
	}

	public Workbook exportXls() {
		// 创建 excel 文件
		Workbook wb = new HSSFWorkbook();
		// 创建工作簿
		Sheet sheet = wb.createSheet();
		// 创建行，参数数行索引
		Row Headrow = sheet.createRow(0);
		// 创建第一行单元格, 并设置单元格数据
		Headrow.createCell(0).setCellValue("名称");
		Headrow.createCell(1).setCellValue("邮箱");
		Headrow.createCell(2).setCellValue("年龄");
		// 遍历设置每行数据
		List<Employee> employees = employeeMapper.list();
		for (int i = 0; i < employees.size(); i++) {
			Employee employee = employees.get(i);
			// 每一个员工一行
			Row row = sheet.createRow(i + 1);
			row.createCell(0).setCellValue(employee.getName());
			row.createCell(1).setCellValue(employee.getEmail());
			row.createCell(2).setCellValue(employee.getAge());
		}
		return wb;
	}

	public void importXls(Workbook wb) {
		Sheet sheet = wb.getSheetAt(0);
		int lastRowNum = sheet.getLastRowNum();

		for (int i = 1; i <= lastRowNum; i++) {
			Employee employee = new Employee();
			Row row = sheet.getRow(i);
			employee.setName(row.getCell(0).getStringCellValue());
			employee.setAge(new Double(row.getCell(2).getNumericCellValue()).intValue());
			employee.setEmail(row.getCell(1).getStringCellValue());
			employee.setPassword(new Md5Hash("1",employee.getName()).toString());

			employeeMapper.insert(employee);
		}
	}

	public List<Employee> selectByRoleSn(String[] roleSns) {
		return employeeMapper.selectByRoleSn(roleSns);
	}


}
